﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Linq.SqlClient;
using System.Collections.Generic;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Utility;

namespace WebHoiChoMuaBan.Controllers.Helpers
{
    public class BannerAdHelper : HelperBase
    {
        private BannerAd banner = null;

        public BannerAd Banner
        {
            get { return banner; }
            set { banner = value; }
        }
        public BannerAdHelper(int bannerId)
            : base()
        {
            getBanner(bannerId);
        }

        public BannerAdHelper()
            : base()
        {
        }

        public BannerAd getBanner(int id)
        {
            this.banner = this.dataContext.BannerAds.SingleOrDefault(m => m.BannerId == id);

            if (this.banner != null)
                this.banner.IsEdit = true;
            return this.banner;
        }

        public struct BannerAdResult
        {
            public int id;
            public string mota;
            public string hinhanh;
            public DateTime? ngaydang;
            public DateTime? ngayketthuc;
            public string luotclick;
            public bool status;
        }

        public BannerAdList getAllBannerAds(int page, int rowPerPage)
        {
            IQueryable<BannerAdResult> newMembers = from m in this.dataContext.BannerAds
                                                    orderby m.BannerId ascending
                                                    select new BannerAdResult
                                                    {
                                                        id = m.BannerId,
                                                        mota = m.BannerText,
                                                        ngaydang = m.BannerStartDate,
                                                        ngayketthuc = m.BannerEndDate,
                                                        luotclick = m.BannerLink,
                                                        status = m.BannerStatus,
                                                        hinhanh = m.BannerImg
                                                    };
            return CreateListBannerAd(page, rowPerPage, newMembers);
        }

        private BannerAdList CreateListBannerAd(int page, int rowPerPage, IQueryable<BannerAdResult> newCustomer)
        {
            List<BannerAdResult> pagedNewCustomers = newCustomer.Skip((page - 1) * rowPerPage).Take(rowPerPage).ToList();

            int totalPages = (int)Math.Ceiling((double)newCustomer.Count() / rowPerPage);

            BannerAdList lstMem = new BannerAdList(totalPages);
            BannerAd mem;
            foreach (var i in pagedNewCustomers)
            {
                mem = new BannerAd();
                mem.BannerId = i.id;
                mem.BannerText = i.mota;
                mem.BannerStartDate = i.ngaydang;
                mem.BannerEndDate = i.ngayketthuc;
                mem.BannerLink = i.luotclick;
                mem.BannerStatus = i.status;
                mem.BannerImg = i.hinhanh;
                lstMem.Add(mem);
            }
            return lstMem;
        }

        public void saveBannerAd(BannerAd cus)
        {
            if (cus.BannerId == 0)
                this.dataContext.BannerAds.InsertOnSubmit(cus);
            else
            {
                BannerAd curr = getBanner(cus.BannerId);

                curr.BannerEndDate = cus.BannerEndDate;
                curr.BannerImg = cus.BannerImg;
                curr.BannerLink = cus.BannerLink;
                curr.BannerPageId = cus.BannerPageId;
                curr.BannerPosition = cus.BannerPosition;
                curr.BannerStartDate = cus.BannerStartDate;
                curr.BannerStatus = cus.BannerStatus;
                curr.BannerText = cus.BannerText;
                curr.BannerType = cus.BannerType;
                curr.CusId = cus.CusId;
            }
            Save();
        }

        public void DeleteBannerAd(string id)
        {
            BannerAd en = (from e in this.dataContext.BannerAds where e.BannerId == int.Parse(id) select e).FirstOrDefault();
            this.dataContext.BannerAds.DeleteOnSubmit(en);
            Save();
        }
        public BannerAd getCustomer(string id)
        {
            BannerAd en = (from e in this.dataContext.BannerAds where e.BannerId == int.Parse(id) select e).FirstOrDefault();
            return en;
        }

        public List<BannerAd> getBannerAdPosition()
        {
            List<BannerAd> result = (from e in this.dataContext.BannerAds orderby e.BannerPosition ascending select e ).ToList<BannerAd>();
            return result;
        }

        public void Save()
        {
            this.dataContext.SubmitChanges();
        }
    }
}
